#include <QFontDialog>
#include <QDebug>
#include "dialogdoubleaxisproperties.h"
#include "ui_dialogdoubleaxisproperties.h"

DialogDoubleAxisProperties::DialogDoubleAxisProperties(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogDoubleAxisProperties)
{
    ui->setupUi(this);
    _font = QFont();
}

DialogDoubleAxisProperties::~DialogDoubleAxisProperties()
{
    delete ui;
}

double DialogDoubleAxisProperties::min() const
{
    bool isCastable = false;
    double min = ui->lineMinimum->text().toDouble(&isCastable);
    if (isCastable) return min;
    else return 0;
}

double DialogDoubleAxisProperties::max() const
{
    bool isCastable = false;
    double max = ui->lineMaximum->text().toDouble(&isCastable);
    if (isCastable) return max;
    else return 0;
}

double DialogDoubleAxisProperties::increment() const
{
    bool isCastable = false;
    double increment = ui->lineIncrement->text().toDouble(&isCastable);
    if (isCastable) return increment;
    else return 0;
}

int DialogDoubleAxisProperties::minorTicks() const
{
    return ui->spinMinorTicks->value();
}



bool DialogDoubleAxisProperties::isLabelsVisible() const
{
    return ui->groupLabels->isChecked();
}

bool DialogDoubleAxisProperties::isMajorTicksVisible() const
{
    return ui->checkShowIncr->isChecked();
}

bool DialogDoubleAxisProperties::isMinorTicksVisible() const
{
    return ui->checkShowMinor->isChecked();
}

bool DialogDoubleAxisProperties::isMajorGridVisible() const
{
    return ui->checkGridIncr->isChecked();
}

bool DialogDoubleAxisProperties::isMinorGridVisible() const
{
    return ui->checkGridMinor->isChecked();
}


bool DialogDoubleAxisProperties::setMinimum(double min)
{
    ui->lineMinimum->setText(QString("%1").arg(min));
    return true;
}

bool DialogDoubleAxisProperties::setMaximum(double max)
{
    ui->lineMaximum->setText(QString("%1").arg(max));
    return true;
}

bool DialogDoubleAxisProperties::setIncrement(double increment)
{
    ui->lineIncrement->setText(QString("%1").arg(increment));
    return true;
}

bool DialogDoubleAxisProperties::setMinorTicks(int ticks)
{
    if (ticks < 1) return false;
    ui->spinMinorTicks->setValue(ticks);
    return true;
}

bool DialogDoubleAxisProperties::setMinorTicksVisible(bool state)
{
    if (state == ui->checkShowMinor->isChecked()) return false;
    ui->checkShowMinor->setChecked(state);
    return true;
}

bool DialogDoubleAxisProperties::setMinorGridVisible(bool state)
{
    if (state == ui->checkGridMinor->isChecked()) return false;
    ui->checkGridMinor->setChecked(state);
    return true;
}

bool DialogDoubleAxisProperties::setMajorTicksVisible(bool state)
{
    if (state == ui->checkShowIncr->isChecked()) return false;
    ui->checkShowIncr->setChecked(state);
    return true;
}

bool DialogDoubleAxisProperties::setMajorGridVisible(bool state)
{
    if (state == ui->checkGridIncr->isChecked()) return false;
    ui->checkGridIncr->setChecked(state);
    return true;
}

bool DialogDoubleAxisProperties::setLabelsVisible(bool state)
{
    if (state == ui->groupLabels->isChecked()) return false;
    ui->groupLabels->setChecked(state);
    return true;
}

bool DialogDoubleAxisProperties::setLabelsOrientation(
    StatS::SDoubleAxis::Orientation orientation)
{
    //TODO: implement error checking
    switch(orientation) {
    case StatS::SDoubleAxis::Vertical:
        ui->comboOrientation->setCurrentIndex(0);
        break;
    case StatS::SDoubleAxis::Horizontal:
        ui->comboOrientation->setCurrentIndex(1);
        break;
    }
    return true;
}

StatS::SDoubleAxis::Orientation DialogDoubleAxisProperties::labelsOrientation() const
{
    switch(ui->comboOrientation->currentIndex()) {
    case 0: return StatS::SDoubleAxis::Vertical;
    case 1: return StatS::SDoubleAxis::Horizontal;
    default: qWarning() << "Orientation not defined: "
                        << ui->comboOrientation->currentIndex();
        return StatS::SDoubleAxis::Vertical;
    }
}

void DialogDoubleAxisProperties::on_pushFont_clicked()
{
    bool dialogResult = false;
    QFont font = QFontDialog::getFont(
                &dialogResult, _font,
                this, "Choose labels font...");
    if (!dialogResult) return;
    setLabelsFont(font);
    return;
}

bool DialogDoubleAxisProperties::setLabelsFont(QFont font)
{
    if (_font == font) return false;
    _font = font;
    return true;
}

QFont DialogDoubleAxisProperties::labelsFont() const
{
    return _font;
}

bool DialogDoubleAxisProperties::setLabelsFormat(char format)
{
    switch (format) {
    case 'g':
        ui->comboFormat->setCurrentIndex(0);
        break;
    case 'f':
        ui->comboFormat->setCurrentIndex(1);
        break;
    case 'e':
        ui->comboFormat->setCurrentIndex(2);
        break;
    default:
        qWarning() << "Invalid label format: "
                   << format;
        return false;
    }
    return true;
}

char DialogDoubleAxisProperties::labelsFormat() const
{
    switch (ui->comboFormat->currentIndex()) {
    case 0: return 'g';
    case 1: return 'f';
    case 2: return 'e';
    default: qWarning() << "Invalid comboFormat index: "
                        << ui->comboFormat->currentIndex();
    }
    return 'g';
}

int DialogDoubleAxisProperties::labelsPrecision() const
{
    return ui->spinPrecision->value();
}

bool DialogDoubleAxisProperties::setLabelsPrecision(int precision)
{
    if (precision == ui->spinPrecision->value() || precision < 0)
        return false;
    ui->spinPrecision->setValue(precision);
    return true;
}
